using System;
using System.Web;
using System.Text;
using System.Collections;

using Utility = PHSRAG.Utility;
using BO = PHSRAG.Insight21.BusinessObjects;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using PHSRAG.Insight21.SearchContext;
using Constants = PHSRAG.Insight21.InsightUtilities.Constants;
using AgreementsConstants = PHSRAG.Insight21.InsightUtilities.Constants.AgreementsConstants;

namespace PHSRAG.Insight21.Agreements
{
    public class AgreementMgr
    {
        #region Constants
        private const string SortDirectionKey = "SortAscending_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string SortColumnKey = "SortColumn_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string CurrentPageIndexKey = "CurrentPageIndex_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string FundNumberKey = "FundNumber_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string CostOptionKey = "CostOption_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string SearchCriteriaVersionKey = "SearchCriteriaVersion_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string ExcelReportControlFilePath = "../../Agreements/Reports/FinancialSummaryExcel.ascx";
        #endregion

        Policy.ICachePolicy FrameWrkCacheObject = null;


        public AgreementMgr()
        {
            FrameWrkCacheObject = new Policy.FrameWorkCachePolicy();
        }

        public void PurgeCache(string key)
        {
            FrameWrkCacheObject.UnCache(key);
        }

        /// <summary>
        /// This method retreives the financial summary collection either from the cache or builds it from the database. 
        /// If the cache reference is null, it gets it from the database and inserts the collection into the cache object.
        /// </summary>
        /// <param name="PageUrl"></param>
        /// <param name="GLPostAsOfDate"></param>
        /// <param name="cachekey">Key used to cache the resultset</param>
        /// <param name="bEmpty"></param>
        /// <returns>Financial summaries collection. </returns>
        public Agreements.FinancialSummaries GetFinancialSummaries(string PageUrl, string GLPostAsOfDate, 
            string cachekey, out bool bEmpty)
        {
            bEmpty = false;

            //Get the resultset from the cache
            Agreements.FinancialSummaries financialSummaries = (Agreements.FinancialSummaries)FrameWrkCacheObject.GetCachedObject(cachekey);

            Policy.ICachePolicy cachePolicy = (Policy.ICachePolicy)HttpContext.Current.Session[Policy.Names.CachePolicyKey];
            CommonSearchContext searchContext = (CommonSearchContext)cachePolicy.GetCachedObject(CommonSearchContext.CacheKey);
            SearchCriteria searchCriteria = searchContext.GetSearchCriteria(PageUrl);

            int searchVersion = searchCriteria.Version;

            if (financialSummaries == null || ((searchContext != null) && (searchVersion != SearchCriteriaVersion)))
            {
                bEmpty = (searchVersion==0);

                SearchCriteriaVersion = searchVersion;
                searchCriteria[Constants.PIIDKey] = searchCriteria["piName"];
                financialSummaries = new Agreements.FinancialSummaries();

                financialSummaries.Load(Utility.DataAccess.GetConnectionString(), searchCriteria,
                    GLPostAsOfDate, IncludeIndirectCosts,
                    SortColumn, SortAscending, HttpContext.Current.Session.SessionID);

                //Insert into the cache
                FrameWrkCacheObject.Cache(cachekey, financialSummaries);                 
            }
            return financialSummaries;
        }

        private Policy.ICachePolicy GetPageCachePolicy
        {
            get
            {
                return (Policy.ICachePolicy)HttpContext.Current.Session[Policy.Names.CachePolicyKey];
            }
        }
        /// <summary>
        /// Gets/sets an integer value indicating the search criteria version. 
        /// The version number changes when the search criteria changes.  This signals the page to re-build its
        /// cache.
        /// </summary>
        /// <remarks>
        /// This and all properties required to re-establish the page are stored as name / value pairs in
        /// a hash table stored in the page cache under the key Keys.FinancialSummaryPageKey.
        /// This enables the page to re-establish its state with minimal IO to what it was when
        /// the user last visited it.
        /// </remarks>
        public int SearchCriteriaVersion
        {
            get
            {
                Hashtable h = GetPageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                return (h == null || h[SearchCriteriaVersionKey] == null) ? 0 : (int)h[SearchCriteriaVersionKey];
            }
            set
            {
                Hashtable h = GetPageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[SearchCriteriaVersionKey] = value;
                GetPageCachePolicy.Cache(Keys.FinancialSummaryPageKey, h);
            }
        }

        /// <summary>
        /// Private boolean property used to get/set if the sort order is ascending or descending.
        /// </summary>
        /// <remarks>
        /// This and all properties required to re-establish the page are stored as name / value pairs in
        /// a hash table stored in the page cache under the key Keys.FinancialSummaryPageKey.
        /// This enables the page to re-establish its state with minimal IO to what it was when
        /// the user last visited it.
        /// </remarks>
        public bool SortAscending
        {
            get
            {
                Hashtable h = GetPageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) return true;
                object sortAscending = h[SortDirectionKey];
                return (sortAscending == null) ? true : (bool)sortAscending;
            }
            set
            {
                Hashtable h = GetPageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[SortDirectionKey] = value;
                GetPageCachePolicy.Cache(Keys.FinancialSummaryPageKey, h);
            }
        }

        /// <summary>
        /// Gets/sets the cost option which when set to true means total cost and when set to false means direct cost.
        /// </summary>
        /// <remarks>
        /// This and all properties required to re-establish the page are stored as name / value pairs in
        /// a hash table stored in the page cache under the key Keys.FinancialSummaryPageKey.
        /// This enables the page to re-establish its state with minimal IO to what it was when
        /// the user last visited it.
        /// </remarks>
        public bool IncludeIndirectCosts
        {
            get
            {
                Hashtable h = GetPageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                return (h == null || h[CostOptionKey] == null) ? true : (bool)h[CostOptionKey];
            }
            set
            {
                Hashtable h = GetPageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[CostOptionKey] = value;
                GetPageCachePolicy.Cache(Keys.FinancialSummaryPageKey, h);
            }
        }

        /// <summary>
        /// Private boolean property used to get/set the base sorting column.
        /// </summary>
        /// <remarks>
        /// This and all properties required to re-establish the page are stored as name / value pairs in
        /// a hash table stored in the page cache under the key Keys.FinancialSummaryPageKey.
        /// This enables the page to re-establish its state with minimal IO to what it was when
        /// the user last visited it.
        /// </remarks>
        public string SortColumn
        {
            get
            {

                Hashtable h = GetPageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) return "CurrentCashBalance";
                object sortColumn = h[SortColumnKey];
                return (sortColumn == null) ? "CurrentCashBalance" : (string)sortColumn;
            }
            set
            {
                Hashtable h = GetPageCachePolicy.GetCachedObject(Keys.FinancialSummaryPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[SortColumnKey] = value;
                GetPageCachePolicy.Cache(Keys.FinancialSummaryPageKey, h);
            }
        }

    }
}
